package main.com.oaer.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import main.com.oaer.common.HibernateDaoUtil;
import main.com.oaer.dao.UserRoleDao;
import main.com.oaer.model.UserRole;

@Repository
public class UserRoleDaoImpl implements UserRoleDao {

	@Autowired
	HibernateDaoUtil hibernateDaoUtil;
	
	@Override
	public void add(UserRole userRole) {
		if(userRole!=null){
			hibernateDaoUtil.save(userRole);
		}
	}

	@Override
	public UserRole findRoleByUserId(Long userId) {
		if(userId==null) return null;
		String hql = "from " +
				" UserRole  userRole " +
				" where " +
				" userRole.userId=?";
		return (UserRole) hibernateDaoUtil.findEntityByHQL(hql,userId);
	}

	@Override
	public void delete(UserRole userRole) {
		if(userRole!=null){
			hibernateDaoUtil.delete(userRole);

		}
	}

	@Override
	public void deleteByRoleId(Long roleId) {
		if(roleId!=null){
			String hql = "delete from " +
			" UserRole userRole " +
			" where " +
			" userRole.roleId=?";
	        hibernateDaoUtil.executeUpdateByHQL(hql, roleId);
		}
		
	}

	@Override
	public void deleteByUserId(Long userId) {
		if(userId!=null){
			String hql = "delete from " +
			" UserRole userRole " +
			" where " +
			" userRole.userId=?";
	        hibernateDaoUtil.executeUpdateByHQL(hql, userId);
		}
		
	}

}
